package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.instance.entity.Interpolate;
import com.a01_2021.thousandstimes.instance.mapper.InterpolateMapper;
import com.a01_2021.thousandstimes.instance.service.IInterpolateAttachmentService;
import com.a01_2021.thousandstimes.instance.service.IInterpolateService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class InterpolateServiceImpl extends ServiceImpl<InterpolateMapper, Interpolate> implements IInterpolateService {

    @Resource
    InterpolateMapper interpolateMapper;

    @Resource
    IInterpolateAttachmentService interpolateAttachmentService;

    /**
     * 添加内推
     *
     * @param userId       用户id
     * @param introduction 推荐理由
     * @param attachment   附件列表
     * @return HashMap
     * @throws CustomException 不应当抛出该异常
     */
    @Override
    public HashMap<String, Object> interpolate(long userId, String introduction, List<String> attachment) throws CustomException {
        //添加内推
        Interpolate interpolate = new Interpolate(null, userId, introduction);
        interpolateMapper.insert(interpolate);
        //添加内推附件
        interpolateAttachmentService.addAttachments(interpolate.getId(), attachment);
        //构造返回信息
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("interpolate", interpolate);
        resultMap.put("attachment", attachment);
        return resultMap;
    }

    /**
     * 用于查询该内推是否存在
     *
     * @param interpolateId 内推id
     * @return 是否存在
     */
    public boolean isInterpolate(long interpolateId) {
        QueryWrapper<Interpolate> interpolateQueryWrapper = new QueryWrapper<>();
        interpolateQueryWrapper.eq("id", interpolateId);
        int count = interpolateMapper.selectCount(interpolateQueryWrapper);
        return count != 0;
    }
}
